/*
 * @Author: 0x9DEFA478
 * @Date: 2025-08-02 20:46:13
 * @LastEditTime: 2025-08-10 22:31:12
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * 
 */
#ifndef hHMakeLog_HPP_
#define hHMakeLog_HPP_
#include <string>
#include <thread>
#include <atomic>
#include "H_FIFO.hpp"
#include "H_Pointer.hpp"


namespace HMake{

  class Log{

    public:
      using Pointer=H::PointerShare<Log>;

      template<typename... T_Arg>
      static Log::Pointer New(T_Arg&&... Args){
        return Pointer(new Log(std::forward<T_Arg>(Args)...));
      }

      enum class Type{
        Default,
        TaskDone,
        TaskDoneSkip
      };

      struct Message{
        bool Valid;
        Type type;
        std::string text;

        Message():Valid(false){};
        Message(Type type,const std::string& text):Valid(true),type(type),text(text){}
        ~Message()=default;

      };

    public:

      Log(std::ostream& ostream)noexcept;
      ~Log();

      void WriteLog(const std::string& text,Type type=Type::Default);

      void ResetCount(H::size AllCount);

      void Start();
      void Stop();

      void Lock()noexcept;
      void Unlock()noexcept;

      H::size SkipCount()const noexcept{return skipCount;}
      H::size UpdateCount()const noexcept{return updateCount;}

    private:
      H::FIFO<Message> fifo;
      H::Lock ostreamLock;
      H::Event emptyEvent;
      std::ostream& ostream;
      std::atomic_bool isStop;
      std::thread* thread;

      H::size allCount;
      H::size currentCount;
      H::size skipCount;
      H::size updateCount;

      std::string endString;
  };


}


#endif //hHMakeLog_HPP_
